#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
int get_Sum(int a, int b)
{
    int sum = 0;
    while (a)
    {
        sum += a % 10;
        a /= 10;
    }
    while (b)
    {
        sum += b % 10;
        b /= 10;
    }
    return sum;
}

int movingCount(int threshold, int rows, int cols)
{
    int res = 0;
    queue<pair<int, int>> queue;
    int dir[4][2] = {
        {-1, 0},
        {0, -1},
        {1, 0},
        {0, 1}
    };
    int vis[rows][cols];
    //初始化
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < cols; j++){
            vis[i][j] = 0;
        }
    }
    queue.push({0, 0});
    while (!queue.empty())
    {
        auto p = queue.front();
        queue.pop();
        if(vis[p.first][p.second] || get_Sum(p.first, p.second) > threshold)
            continue;
        res++;
        vis[p.first][p.second] = 1;
        for (int i = 0; i < 4; i++)
        {
            int x = p.first + dir[i][0];
            int y = p.second + dir[i][1];
            if (x >= 0 && y >= 0 && x < rows && y < cols && !vis[x][y])
                queue.push({x, y});
        }
    }
    return res;
}

int main(){
    cout<<movingCount(7, 4, 5)<<endl;
    return 0;
}